import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue')
  }, {
    path: '/goods',
    name: 'Goods',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue'),
    redirect:'/user/cart',
    // 购物车页面配置
    children:[
      {path:'cart', component: () => import('../components/user/Cart.vue')}
    ]
    // beforeEnter:(to,from,next)=>{
    //   console.log('路由独享守卫');
    //   let token=localStorage.getItem('x-auth-token');
    // if(token){
    //   next();
    // }else{
    //   store.dispatch('showToast/asyncShowToast',{
    //     isShow:true,
    //     type:'danger',
    //     title:'请先登录'
    //   })
    // }
    // }
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/free',
    name: 'Free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/Details.vue')
  },
  {
    path:'*',
    component:()=>import('../components/Error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局守卫
// router.beforeEach((to,from,next)=>{
//   console.log('守卫',to,from);
//   if(to.path=='/user'){
//     let token=localStorage.getItem('x-auth-token');
//     if(token){
//       next();
//     }else{
//       // alert('请先登录');8iiii
//       store.dispatch('showToast/asyncShowToast',{
//         isShow:true,
//         type:'danger',
//         title:'请先登录'
//       })
//     }
//   }else{
//     next();
//   }
// })
// 解决路由重复点击bug
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};

export default router
